package com.liunian.test;

import java.util.Arrays;

public class MonotoneIncreasingDigits {

	public int monotoneIncreasingDigits(int n) {
		char[] chars = Integer.toString(n).toCharArray();
		int left = Integer.MAX_VALUE;
		for (int i = chars.length - 1; i > 0; i--) {
			if (chars[i - 1] > chars[i]) {
				chars[i - 1] -= 1;
				left = i;
			}
		}
		for (int i = left; i < chars.length; i++) {
			chars[i] = '9';
		}
		String s = chars[0] == '0' && chars.length != 1 ? String.valueOf(chars).substring(1) : String.valueOf(chars);
		return Integer.parseInt(s);
	}

	public static void main(String[] args) {
		MonotoneIncreasingDigits digits = new MonotoneIncreasingDigits();
		System.out.println(digits.monotoneIncreasingDigits(0));
	}

}
